package com.justnow.offer;

/**
 * @author justnow
 * Created on 2020-10-14
 * Description
 */
public class XInhuasan01 {
    public int prime (int i) {
        // write code here
        if (i < 1 || i > 10000) {
            return -1;
        }

        if (i == 1 || i == 10000) {
            return 0;
        }

        int left;
        for (left = i - 1; left > 1; left--) {
            if (isSpecial(left)) {
                break;
            }
        }

        if (left == 1) {
            return 0;
        }
        int right;
        for (right = i + 1; right < 10000 ; right++) {
            if (isSpecial(right)) {
                break;
            }
        }
        if (right == 10000) {
            return 0;
        }

        return right - left;
    }

    public boolean isSpecial(int number) {

        for (int i = 2; i <=  Math.sqrt(number); i++) {
            if (number % 2 == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {

    }
}
